export const formatTime = (time: number, type: string = "time", separator="/") => {
    if((time + "").length < 13) {
        time = time * 1000;
    }
    const current = new Date(time);
    const fullYear = current.getFullYear();
    const month = current.getMonth() + 1;
    const monthStr = month < 10 ? `0${month}`: month;
    const date = current.getDate();
    const dateStr = date < 10 ? `0${date}`: date;
    if(type === "month") {
        return `${fullYear}${separator}${monthStr}`;
    }
    if(type === "date") {
        return `${fullYear}${separator}${monthStr}${separator}${dateStr}`;
    }
    if(type === "onlyMonthAndDate") {
        return `${monthStr}${separator}${dateStr}`;
    }
    const hours = current.getHours();
    const hourStr = hours >= 10 ? hours: `0${hours}`;
    if(type === "hour") {
        return `${fullYear}${separator}${monthStr}${separator}${dateStr} ${hourStr}`;
    }
    const minutes = current.getMinutes();
    const minuteStr = minutes >= 10 ? minutes: `0${minutes}`;
    if(type === "minute") {
        return `${fullYear}${separator}${monthStr}${separator}${dateStr} ${hourStr}:${minuteStr}`;
    }
    const seconds = current.getSeconds();
    const secondStr = seconds >= 10 ? seconds: `0${seconds}`;
    if(type === "time") {
        return `${fullYear}${separator}${monthStr}${separator}${dateStr} ${hourStr}:${minuteStr}:${secondStr}`;
    }
};

export const getMonthRangeTime = (startMonth: number, endMonth: number = 0) => {
  const curentTime = new Date().getTime();
  const monthTime = 30 * 60 * 60 * 24 * 1000;
  const startTime = curentTime + startMonth * monthTime;
  const endTime = curentTime + endMonth * monthTime;
  return [startTime, endTime];
};

// 农历日期
export const formatLumarTime = (date: Date, type = "all") => {
    const dateString = new Intl.DateTimeFormat('zh-u-ca-chinese', { dateStyle: 'medium' }).format(date);
    if(type === "all") {
        return dateString;
    }
    let index = 0;
    if (type === "onlyDate") {
        index = dateString.indexOf("月");
    } else {
        index = dateString.indexOf("年");
    }
    return dateString.slice(index+1);
};
